//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System;
    using System.Windows;
    using ManagementStudioModels;
    using ManagementStudioServices.Interfaces;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using Microsoft.Practices.Unity;
    using SR = Microsoft.Research.DataLayer;

    /// <summary>
    /// The persenter for the left pane activity tree.
    /// </summary>
    public class ActivityLeftPanePresenter : RegistryManagerLeftPaneItemPresenterBase
    {
        /// <summary>
        /// Gets or sets the root model.
        /// </summary>
        /// <value>The root model.</value>
        public override BaseRegMgrModel RootModel
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ActivityLeftPanePresenter"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="leftPane">The left pane.</param>
        /// <param name="service">The service.</param>
        public ActivityLeftPanePresenter(IUnityContainer container, ActivitiesTree leftPane, IRegistryManagerService service)
            : base(container, service)
        {
            this.LeftPaneView = leftPane;
            leftPane.DataContext = this;
        }

        #region IRegistryManagerLeftPaneItemPresenter Members

        /// <summary>
        /// Gets or sets the search view.
        /// </summary>
        /// <value>The search view.</value>
        public override UIElement SearchView
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the left pane view.
        /// </summary>
        /// <value>The left pane view.</value>
        public override UIElement LeftPaneView
        {
            get;
            set;
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public override void Initialize()
        {
            try
            {                
                if (this.LeftPaneTreeItems.Count > 0)
                {
                    this.LeftPaneTreeItems.Clear();
                }

                this.BuildTree();
            }
            catch (TridentCustomException ex)
            {
                TridentErrorHandler.HandleTridentCustomExceptionInUI(ex);
            }
            catch (Exception ex)
            {
                TridentErrorHandler.HandleKnownExceptionsInUI(ex);                
            }
        }

        #endregion

        /// <summary>
        /// Builds the Activity Tree
        /// </summary>
        private void BuildTree()
        {            
             SR.Namespace activityRoot = this.RegistryManagerService.GetActivityCategories();
             CategoryViewModel activityRootModel = new CategoryViewModel(activityRoot, null, CategoryType.Activity, this.RegistryManagerService.GetSystemPolicy(), true);
             this.LeftPaneTreeItems.Add(activityRootModel);
             this.RootModel = activityRootModel;            
        }
    }
}
